class StringUtil {
	public constructor() {
	}

	public static stringIndexColor: string[] = ["w", "g", "b", "p", "r", "o", "y"];

	/**
	 * 解析字符串中的颜色符号
	 */
	public static parseTextColor(str:string):string{
		let arr = StringUtil.stringIndexColor;
		for(let k in arr){
			let exp = '@' + arr[k];
			let type = str.indexOf(exp);
			if(type != -1){
				str = str.replace(exp, '');
			}
		}
		return StringUtil.getColorString(str, 0xffff00);
	}

	/**
	 * 获取带颜色的字符串
	 */
	public static getColorString(str:string, color:number){
		return '<font color=' + color + '>' + str + '</font>';
	}

	/**
	 * 获取textFlow类型字符串
	 */
	public static getTextFlow(str: string):egret.ITextElement[]{
		return new egret.HtmlTextParser().parser(str);
	}

	/**
	 * 将Json转为HTTP形式的字符串 key=value&key=value..
	 */
	public static jsonToHttpPostString(json:any){
		let str = '';
		for(let k in json){
			str += k + '=' + json[k] + '&';
		}
		str = str.slice(0, str.length-1); 
		return str.trim();
	}

	/**
	 * 将数组转为下划线拼接形式的字符串 1_2_3.
	 */
	public static arrToHttpLineString(arr){
		let str = '';
		for(let v of arr){
			str += v + '_';
		}
		str = str.slice(0, str.length-1); 
		return str.trim();
	}

		/**
	 * 带文本替换功能的字符串
	 * @param StrID
	 * @param valArr
	 * @return 
	 */
	public static getSwfLangStrVar(strID: string, valArr: string[]): string {
		if (DataJson.tips_data == null) {
			return strID;
		}
		var data: any = DataJson.tips_data[strID];
		if (data == null) {
			return strID;
		}

		var strData: string = data.language_content.toString();
		var indexpre: number;
		var indexback: number;
		var strget: string;

		indexpre = strData.indexOf("{");
		indexback = strData.indexOf("}");

		//下一次搜索的起始偏移量,防止{@}嵌套时，造成死循环
		var nextOffset: number = 0;

		var firstIndex: number;

		var strFlagPre: number;
		var strFlagBack: number;
		var strFlag: string;

		while (indexpre != -1 && indexback != -1) {
			strget = strData.substring(indexpre, indexback + 1);

			firstIndex = strData.indexOf("@", nextOffset);
			//var number: int = int(strData.charAt(strData.indexOf("@", nextOffset) + 1));
			var numberic: number = parseInt(strData.substring(firstIndex + 1, strData.indexOf(":", firstIndex))) - 1;
			if (numberic == null) {
				return "stringError:" + strID;
			}

			//处理填充字符串参数（如果有）
			strFlagPre = strData.indexOf("!#[", nextOffset) + 3;
			if (strFlagPre > 2) {
				//前缀{!#[PeerageRank_]@0:}
				strFlagBack = strData.indexOf("]@", nextOffset);
				strFlag = strData.substring(strFlagPre, strFlagBack);
				valArr[numberic] = StringUtil.getSwfLangStr(strFlag + valArr[numberic]);
			}

			var strreplace: string = valArr[numberic].toString();

			strData = strData.replace(strget, strreplace);

			nextOffset = indexpre + strreplace.length;

			indexpre = strData.indexOf("{", nextOffset);
			indexback = strData.indexOf("}", nextOffset);
		}

		return strData;
	}

	/**
	 * 获取配置好的字符串
	 * @param StrID
	 * @return 
	 * 
	 */
	public static getSwfLangStr(strID: string): string {
		if (DataJson.tips_data == null) {
			return strID;
		}
		var data: any = DataJson.tips_data[strID];
		if (data == null) {
			return strID;
		}
		return data.language_content;
	}

}